<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>预定</title>
    <link rel="stylesheet" href="../icon/iconfont.css">
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/style.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.css">
    <link rel="stylesheet" type="text/css" href="../css/frame1.css">

    <style>
        .aui-body {
            /*margin-top: 2.5rem*/
        }

        .video {
            width: 100%;
            height: 10rem;
            background-color: #000
        }

        .zan_icon.active {
            color: rgb(201, 22, 26);
        }

        .room_photo {
            border-top: 1px solid #EEE;
            padding-top: .5rem;
            margin-top: -.8rem;
        }

        .room_photo:first-child {
            border: 0
        }
        .Housekeeping{
          background: #D59B4F;
          color:#FFF;
          float: right;
          padding: 5px 7px;
          border-radius: 2px;
          margin: 0 8px 0 0;
          font-size: .65rem;
          z-index: 99999;
        }
    </style>
</head>

<body>
    <!-- tab栏 -->
    <!-- <div class="aui-tab">
        <div class="aui-tab-item tapmode aui-active">客房</div>
        <div class="aui-tab-item tapmode">
            <div></div>餐饮</div>
        <div class="aui-tab-item tapmode">其他</div>
    </div> -->

    <div class="aui-body">
        <p id="toast" style="text-align:center;margin-top:1rem;display:none">暂无更多数据!</p>
        <div class="room_photoBox" id="room_photoBox">


        </div>
    </div>
</body>
<script type="text/javascript" src="../lib/zepto.js"></script>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/dns.js"></script>
<script type="text/javascript" src="../script/aui-toast.js"></script>
<script type="text/javascript" src="../lib/artTemplate/template.js"></script>

<script type="text/html" id="getActivityList">
    {{each items as value index}}
    <div class="room_photo" articleID="{{value.id}}" onclick="getActive(this,{{value.id}},'{{value.title}}')">
        <div class="photo_header">
            <div class="head_portrait">
                <img src="{{value.merchant.img.url}}" alt="" style="width:100%;height:100%">
            </div>
            <div class="site">
                <h4 class="site_name">{{value.merchant.title}}</h4>
                <p class="site_date">{{value.create_time}}</p>
            </div>
        </div>
        <div class="photo_content">
            <h3 class="title">{{value.title}}
              {{if value.action_cate.id == 3}}
              <span class="Housekeeping" onclick="Housekeeping({{value.id}},'{{value.title}}');">客房入住</span>
              {{/if}}
            </h3>
            <div class="photoList" style="width:100%;">
                {{if value.news_type == 0}}
                <div class="aui-row aui-row-padded photoList1" style="width:100%">
                    {{each value.head_img.data as val ind}}
                    <div class="aui-col-xs-4" imgattr="{{val.filepath}}" onclick="bigPhoto(this)" style="overflow:hidden;text-align:center;">
                        <img class="imgList" style="width:100%;height:100%;background-image:url('{{val.original_image}}');background-size:cover" />
                    </div>
                    {{/each}}
                </div>
                {{/if}} {{if value.news_type == 1}}
                <!-- <div class="aui-row aui-row-padded"> -->
                <video src="{{value.video_id.url}}" class="video" poster="9999.jpg" controls x5-playsinline="" x5-video-orientation="landscape" playsinline="" webkit-playsinline="" style='background-image:url("{{value.head_img.data[0].filepath}}");background-size:100%'></video>
                <!-- </div> -->
                {{/if}}
            </div>
            <div class="footer" style="font-size:.7rem; color:rgb(150, 151, 151);">
                <span class="read">{{value.read_num}}阅读</span>
                <div class="footer_right" articleID="{{value.id}}">
                    <span class="iconfont icon-pinglunzhuanhuan title" articleID="{{value.id}}" style="font-size:.9rem;"></span>&nbsp;<span style="font-size:.7rem;" class="comment comment{{value.id}}">{{value.comment_num}}</span>
                    <span class="zan" onclick="getzan(this)">
                {{if value.tid.is_awesome=="yes"}}
                <span style="font-size:.7rem;" class="iconfont icon-dianzan zan_icon active"></span>&nbsp;<span class="like">{{value.awesome_num+value.virtual_awesome_num}}</span> {{/if}} {{if value.tid.is_awesome=="no"}}
                    <span style="font-size:.7rem;" class="iconfont icon-dianzan zan_icon"></span>&nbsp;<span class="like">{{value.awesome_num+value.virtual_awesome_num}}</span> {{/if}}
                    </span>
                </div>

            </div>
        </div>
    </div>
    {{/each}}
</script>
<script>
    apiready = function() {
        // 监听评论
        api.addEventListener({
            name: "comment2_reload"
        }, function(ret, err) {
            var articleID = localStorage.getItem("articleID");
            var val_comment = $(".comment" + articleID).html();
            val_comment++
            $(".comment" + articleID).html(val_comment)
        });
        api.addEventListener({
            name: "tab_list"
        }, function(ret, err) {
            var type = ret.value.key1;
            getActivity(type)
        });
        refreshList();
    }
    var toast = new auiToast({})

  // 客房入住
  function Housekeeping (id,title){
    api.openWin({
        name: 'room_list',
        url: './frame1/room_list.html',
        pageParam: {
          'roomid':id,
          'roomtitle':title,
        }, //页面代参数
    });
  }

    // 活动列表渲染
    function getActivity(type) {
        var token = localStorage.getItem("token");
        $.ajax({
            type: "get",
            data: {
                page: 1,
                limit: 500,
                type: type
            },
            beforeSend: function(request) {
                request.setRequestHeader("token", token)
            },
            url: dns + "action/read",
            success: function(res) {
              console.log(JSON.stringify(res));
                if (res.count == 0) {
                    $("#toast").css("display", "block");
                } else {
                    $("#toast").css("display", "none")
                }
                var html = template("getActivityList", {
                    "items": res.data
                });
                document.getElementById("room_photoBox").innerHTML = html;
                // 适配video宽高
                var width = $(".video").width();
                var height = width / 16 * 9;
                $(".video").height(height)
                    // 切换底部tab时暂停视频
                api.addEventListener({
                    name: "video"
                }, function(ret, err) {
                    getvideo()
                })
            }
        })
    };
    // 跳转文章详情
    function getActive(e,id,title) {
        var articleID = $(e).attr("articleID");
        localStorage.setItem("articleID", articleID);
        // 跳转到其它页面暂停当前页面的视频
        getvideo()
        api.openWin({
            name: 'details',
            url: './frame1/frame1_details.html',
            pageParam: {
              'roomid':id,
              'roomtitle':title,}, //页面代参数
            rect: {
                x: 0,
                y: 0,
                w: api.winWidth,
                h: api.winHeight
            }
        });
    }

    // 点赞功能实现
    function getzan(e) {
        //如果提供了事件对象，则这是一个非IE浏览器
        if (e && e.stopPropagation) {
            //因此它支持W3C的stopPropagation()方法
            e.stopPropagation();
        } else {
            //否则，我们需要使用IE的方式来取消事件冒泡
            window.event.cancelBubble = true;
        }
        // 获取此时的毫秒数
        var newTime = new Date();
        var getnewTime = newTime.getTime();
        // 获取登陆时的毫秒数
        var getloginTime = localStorage.getItem("getloginTime");
        var Time = getnewTime - getloginTime;
        var token = localStorage.getItem("token");
        var cacheTime = localStorage.getItem("cacheTime");
        if (Time > cacheTime || !token) {
            api.confirm({
                title: "当前游客状态",
                msg: "需要前往登录页面,是否继续?",
                buttons: ["是", "否"]
            }, function(ret, err) {
                if (ret.buttonIndex == 1) {
                    toast.loading({
                        title: "跳转中",
                        duration: 2000
                    }, function(ret) {
                        setTimeout(function() {
                            toast.hide();
                        }, 3000)
                    });
                    setTimeout(function() {
                        api.openWin({
                            name: 'login',
                            url: './system/login.html',
                            allowEdit: true,
                            pageParam: {}, //页面代参数
                            rect: {
                                x: 0,
                                y: 0,
                                w: api.winWidth,
                                h: api.winHeight
                            }
                        });
                    }, 1000)
                }
            });
            return false;
        }
        var val = $(e).find(".like").text();
        var articleID = $(e).parent().attr("articleID");
        var that = e;
        $.ajax({
            type: "post",
            data: {
                type: 2,
                article: articleID
            },
            beforeSend: function(request) {
                request.setRequestHeader("token", token);
            },
            url: dns + "like/okorno",
            success: function(res) {
                if (res.msg == "成功点赞") {
                    $(that).find(".zan_icon").addClass("active");
                    val++;
                    $(that).find(".like").text(val);
                    toast.custom({
                        title: "成功点赞!",
                        html: '<i class="aui-iconfont aui-icon-laud"></i>',
                        duration: 1500
                    });
                };
                if (res.msg == "取消点赞") {
                    $(that).find(".zan_icon").removeClass("active");
                    val--;
                    $(that).find(".like").text(val);
                }
            }
        })
    }
    // 图片放大
    function bigPhoto(e) {
        //如果提供了事件对象，则这是一个非IE浏览器
        if (e && e.stopPropagation) {
            //因此它支持W3C的stopPropagation()方法
            e.stopPropagation();
        } else {
            //否则，我们需要使用IE的方式来取消事件冒泡
            window.event.cancelBubble = true;
        }
        var ind = $(e).index();
        var imgList = [];
        $(e).parent().find(".aui-col-xs-4").each(function() {
            imgList.push($(this).attr("imgattr"))
        })
        var photoBrowser = api.require('photoBrowser');
        photoBrowser.open({
            images: imgList,
            placeholderImg: 'widget://res/img/apicloud.png',
            bgColor: '#000',
            activeIndex: ind
        }, function(ret, err) {
            if (ret.eventType == 'click') {
                photoBrowser.close()
            }
            if (ret.eventType == 'longPress') {
                var a = Math.floor(Math.random() * 1000);
                var retIndex = ret.index;
                api.confirm({
                    title: "温馨提示",
                    msg: "是否保存图片?",
                    buttons: ["是", "否"]
                }, function(ret, err) {
                    if (ret.buttonIndex == 1) { //确认退出
                        api.download({
                            url: imgList[retIndex],
                            savePath: 'fs://' + a + '.jpg',
                            report: true,
                            cache: false,
                        }, function(ret, err) {
                            if (ret.state == 1) {
                                //下载成功
                                // console.log("下载成功");
                                // 保存图片
                                api.saveMediaToAlbum({
                                    path: ret.savePath
                                }, function(ret, err) {
                                    if (ret.status == true) {
                                        alert("保存成功!")
                                    } else {}
                                });
                            } else {

                            }
                        });
                    } else {
                        return;
                    }
                });
            } else {
                // alert(JSON.stringify(err));
            }
        });
    }

    function getvideo() {
        // 跳转到其它页面暂停当前页面的视频
        var videoList = document.getElementsByTagName("video");
        for (var i = 0; i < videoList.length; i++) {
            if (!videoList[i].paused) {
                videoList[i].pause(); //暂停控制
            }
        }
    }
    //该方法用来下拉刷新
    function refreshList(type) {
        api.setCustomRefreshHeaderInfo({
            bgColor: '#eee',
            images: [
                'widget://image/xiaolong/1.png',
                'widget://image/xiaolong/2.png',
                'widget://image/xiaolong/3.png',
                'widget://image/xiaolong/4.png',
                'widget://image/xiaolong/5.png',
            ],
            tips: {
                pull: '下拉刷新' + '\n',
                threshold: '松开立即刷新' + '\n',
                load: '正在刷新' + '\n'
            }
        }, function() {
            setTimeout(function() {
                api.refreshHeaderLoadDone();
                $.ajax({
                    type: "get",
                    url: dns + "actioncate/read",
                    success: function(res) {
                        var idList = [];
                        var data = res.data;
                        for (var i = 0; i < data.length; i++) {
                            idList.push(data[i].id)
                        };
                        var type = localStorage.getItem("type")
                        getActivity(type)
                    }
                })
            }, 1000);
        });
    }
</script>

</html>
